package org.jeecg.modules.friends.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author cold
 * @data 2024/6/22 23:31
 * @version 1.0
 */
public class NearbyUtils {

    private static final double EARTH_RADIUS = 6371000; // 地球半径，单位为米

    public static double calculateDistance(String lat1, String lon1, String lat2, String lon2) {

        double radLat1 = Math.toRadians(Double.valueOf(lat1));
        double radLat2 = Math.toRadians(Double.valueOf(lat2));
        double a = radLat1 - radLat2;
        double b = Math.toRadians(Double.valueOf(lon1)) - Math.toRadians(Double.valueOf(lon2));

        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
                Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        BigDecimal bd = new BigDecimal(s/1000).setScale(2, RoundingMode.HALF_UP);
        return bd.doubleValue();
    }
}
